<?php 
namespace core;
use core\Model;
class Model{
	// 对外查看sql
	public $sql;
	final public function getSql($sql, $field = '*', $where = [], $order = '', $limit = 0, $table){

		// 只支持增删改查
		if($sql === 'select' || $sql === 'update' || $sql === 'delete' || $sql === 'insert'){
			$sql = 'select';
			if(!empty($filed)) {
				$sql .= $filed;
			}

			$sql .= ' ' . $field . ' from ' . $table;

			if(!empty($where)){
				$condition = 'and';
				if(isset($where['condition'])){
					$condition = $where['condition'];
					unset($where['condition']);
				}
				$sql .= ' where';
				$k = 0;
				$count = count($where);
				foreach ($where as $key => &$value) {
					$symbol = '=';
					if(is_array($value)){
						$symbol = $value[0];
						$value = $value[1];
					}
					if($k == $count - 1) {
						$sql .= ' ' . $key . ' ' . $symbol . ' :' . $key;
					}else{
						$sql .= ' ' . $key . ' ' . $symbol . ' :' . $key . ' ' . $condition;
					}
					$k ++;					
				}
			}

			if(!empty($order)){
				$sql .= ' order by' . $order;
			}

			if(!empty($limit)){
				$sql .= ' limit' . $limit;
			}
			// var_dump($where);die;
			return [$sql, $where];
		}else{
			throw new Exception('不支持此类型数据库操作：' . $sql);			
		}
		
	}

	private function parseWhere($where) {
		return $where[0] . ' ' . $where[1];
	}

}